package com.wyg.epuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyg.epuser.entity.CompanyNum;
import com.wyg.epuser.entity.HotSort;
import com.wyg.epuser.mapper.CompanyNumMapper;
import com.wyg.epuser.service.CompanyNumService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WangYongGan
 * @since 2021-11-12
 */
@Service
public class CompanyNumServiceImpl extends ServiceImpl<CompanyNumMapper, CompanyNum> implements CompanyNumService {

    @Override
    public int getCountOfAllCompany() {
        return baseMapper.getCount();
    }

    @Override
    public CompanyNum getCountOfCompany(String entity) {
        QueryWrapper<CompanyNum> wrapper = new QueryWrapper<>();
        CompanyNum companyNum = getOne(wrapper.eq("name", entity));
        return companyNum;
    }

    @Override
    public HotSort getHotSort(String entity) {
        return getHotSort(entity, new HotSort());
    }

    @Override
    public HotSort getHotSort(String entity, HotSort hotSort) {
        QueryWrapper<CompanyNum> wrapper = new QueryWrapper<>();
        //判断是否有
        if (getOne(wrapper.eq("name", entity)) == null)
            return hotSort;
        CompanyNum countOfCompany = getCountOfCompany(entity);
        int regSort = baseMapper.getRegSort(countOfCompany.getRegNum());
        int poiSort = baseMapper.getPoiSort(countOfCompany.getAllNum() - countOfCompany.getRegNum());
        int allSort = baseMapper.getAllSort(countOfCompany.getAllNum());
        int allNum = baseMapper.getCount();
        hotSort.setRegSort(regSort + 2);
        hotSort.setPoiSort(poiSort + 2);
        hotSort.setAllSort(allSort + 2);
        hotSort.setAllNum(allNum + 2);
        return hotSort;
    }

    @Override
    public boolean exists(String entity) {
        QueryWrapper<CompanyNum> wrapper = new QueryWrapper<>();
        return baseMapper.selectOne(wrapper.eq("name", entity).ne("state", "-1")) != null;
    }


}
